package com.alibaba.otter.manager.web.resource;

import com.alibaba.otter.manager.biz.config.datamatrix.DataMatrixService;
import com.alibaba.otter.manager.web.dto.RestResponse;
import com.alibaba.otter.shared.common.model.config.data.DataMatrix;
import com.alibaba.otter.shared.common.model.config.data.DataMediaPair;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/v1/data-matrix")
@RestController
public class DataMatrixResource {


    private final DataMatrixService dataMatrixService;

    public DataMatrixResource(DataMatrixService dataMatrixService) {
        this.dataMatrixService = dataMatrixService;
    }


    /**
     *  根据id查找树
     */
    @GetMapping("findOne/{id}")
    public RestResponse<DataMatrix> findOne(@PathVariable("id") Long id){
        return RestResponse.ok(dataMatrixService.findById(id));
    }


    /**
     * 根据条件查询列表数据
     */
    @GetMapping("list")
    public RestResponse<List<DataMatrix>> list() throws Exception {
        return RestResponse.ok(dataMatrixService.listAll());
    }


    /**
     * 添加
     */
    @PostMapping("add")
    public RestResponse<Void> add(@RequestBody @Validated DataMatrix dbMediaSource){
        dataMatrixService.create(dbMediaSource);
        return RestResponse.ok();
    }


    /**
     * 编辑
     */
    @PutMapping("edit")
    public RestResponse<Void> edit(@RequestBody @Validated DataMatrix dbMediaSource){
        dataMatrixService.modify(dbMediaSource);
        return RestResponse.ok();
    }


    /**
     * 删除
     */
    @DeleteMapping("delete/{id}")
    public RestResponse<Void> delete(@PathVariable("id") Long id){
        dataMatrixService.remove(id);
        return RestResponse.ok();
    }
}
